<?php
class ConfigItem extends ObjectModel 
{
    public $id;
    public $id_tab;
    public $id_parent;
    public $name;
    public $position;
    public $active;
    
    public $label;
    public $description;
    public $lang_iso;
    
	public $date_add;
    public $date_upd;
    
    public static $definition = array(
        'table' => 'pt_config_item',
        'primary' => 'id_item',
        'multilang' => true,
        'fields' => array(
            'id_tab' => array(
                'type' => self::TYPE_INT,
                'required' => false, 
                'validate' => 'isUnsignedId'
            ),
            'id_parent' => array(
                'type' => self::TYPE_INT,
                'required' => false, 
                'validate' => 'isUnsignedId'
            ),
            'name' => array(
                'type' => self::TYPE_STRING,
                'required' => true,
                'validate' => 'isGenericName', 
                'size' => 64
            ),
            'position' => array(
                'type' => self::TYPE_INT
            ),
            
            'label' => array(
                'type' => self::TYPE_STRING,
                'required' => true,
                'lang' => true, 
                'validate' => 'isGenericName', 
                'size' => 128
            ),
            'description' => array(
                'type' => self::TYPE_STRING,
                'lang' => true,
                'validate' => 'isString',
            ),
            'lang_iso' => array(
                'type' => self::TYPE_STRING,
                'required' => true,
                'lang' => true, 
                'size' => 2,
                'validate' => 'isGenericName',
            ),
            
            'active' => array(
                'type' => self::TYPE_BOOL,
                'required' => true
            ),
            'date_add' =>  array(
                'type' => self::TYPE_DATE, 
                'validate' => 'isDateFormat'
            ),
			'date_upd' => array(
                'type' => self::TYPE_DATE,
                'validate' => 'isDateFormat'
            )
        )
    );
    
    public static function getConfigItems ($active = true, $filter = '')
    {
        $id_lang = Context::getContext()->language->id;
        $sql = '
            SELECT i.*, il.label
            FROM `' . _DB_PREFIX_ . 'pt_config_item` i
            LEFT JOIN `' . _DB_PREFIX_ . 'pt_config_item_lang` il ON (i.`id_item` = il.`id_item` AND il.`id_lang` = '. $id_lang .')
            WHERE 1'. ($active ? ' AND i.`active` = 1' : '') . $filter;
        if ($rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql)) 
        {
            foreach ($rows AS &$row) {
                // Check cases where there the item have a parent
                $row['id_tab'] = (int)$row['id_tab'];
                if (empty($row['id_tab'])) {
                    $id_tab = (int)$row['id_tab'];
                    $id_parent = (int)$row['id_parent'];
                    $fullname = $row['label'];
                    $order = '';
                    
                    // Take parent label until rich parent tab
                    while (!$id_tab && $id_parent) {
                        $parent = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
                            SELECT i.`id_item`, il.label, i.`id_tab`, i.`id_parent`, tl.label AS tab_label, c.`code`, c.`version`
                            FROM `' . _DB_PREFIX_ . 'pt_config_item` i
                            LEFT JOIN `' . _DB_PREFIX_ . 'pt_config_item_lang` il ON (i.`id_item` = il.`id_item` AND il.`id_lang` = '. $id_lang .')
                            LEFT JOIN `' . _DB_PREFIX_ . 'pt_config_tab` t ON (i.`id_tab` = t.`id_tab`)
                            LEFT JOIN `' . _DB_PREFIX_ . 'pt_config_tab_lang` tl ON (t.`id_tab` = tl.`id_tab` AND tl.`id_lang` = '. $id_lang .')
                            LEFT JOIN `' . _DB_PREFIX_ . 'pt_theme_editor` e ON (t.`id_editor` = e.`id_editor`)
                            LEFT JOIN `' . _DB_PREFIX_ . 'pt_cms` c ON (e.`id_cms` = c.`id_cms`)
                            WHERE i.`id_item` = '. $id_parent .''. ($active ? ' AND i.`active` = 1' : '') . '
                        ');
                        $fullname = $parent['label'] .' &gt; '. $fullname;
                        $order = '-A'.$parent['id_item']. $order;
                        if (!empty($parent['id_tab'])) {
                            $fullname = $parent['code'] .' '. $parent['version'] .' &gt; '. $parent['tab_label'] .' &gt; '. $fullname;
                            $order = $parent['id_tab'] . $order .'-B'. (int)$row['id_item'] .'-C'. (int)$row['position'];
                            break;
                        } elseif (empty($parent['id_parent'])) {
                            break;
                        }
                        $id_tab = (int)$parent['id_tab'];
                        $id_parent = (int)$parent['id_parent'];
                    }
                    
                    $row['order'] = $order;
                    $row['fullname'] = $fullname;
                } else {
                    $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
                        SELECT i.`id_item`, il.label, i.`id_tab`, i.`id_parent`, 
                            CONCAT(i.`id_tab`, \'-A\', i.`id_parent`, \'-B\', i.`id_item`, \'-C\', i.`position`) AS `order`, 
                            CONCAT(c.`code`, \' \', c.`version`, \' &gt; \', tl.label, \' &gt; \', il.label) AS fullname
                        FROM `' . _DB_PREFIX_ . 'pt_config_item` i
                        LEFT JOIN `' . _DB_PREFIX_ . 'pt_config_item_lang` il ON (i.`id_item` = il.`id_item` AND il.`id_lang` = '. $id_lang .')
                        LEFT JOIN `' . _DB_PREFIX_ . 'pt_config_tab` t ON (i.`id_tab` = t.`id_tab`)
                        LEFT JOIN `' . _DB_PREFIX_ . 'pt_config_tab_lang` tl ON (t.`id_tab` = tl.`id_tab` AND tl.`id_lang` = '. $id_lang .')
                        LEFT JOIN `' . _DB_PREFIX_ . 'pt_theme_editor` e ON (t.`id_editor` = e.`id_editor`)
                        LEFT JOIN `' . _DB_PREFIX_ . 'pt_cms` c ON (e.`id_cms` = c.`id_cms`)
                        WHERE i.`id_item` = '. $row['id_item'] .''. ($active ? ' AND i.`active` = 1' : '') . '
                    ');
                }
            }
            
            // Tri des items
            // Obtient une liste de colonnes
            $rang = array();
            foreach ($rows as $k=>$v) {
            	//$rang[$k]  = $v['order'];
            	$rang[$k]  = $v['fullname'];
            }
            array_multisort($rang, SORT_ASC, $rows);
            
            return $rows;
        }
        return array();
    }
    
    public static function getIdCmsByIdItem ($id_item) 
    {
        $id_cms = false;
        $sql = '
            SELECT i.`id_tab`, i.`id_parent`, c.`id_cms`
            FROM `' . _DB_PREFIX_ . 'pt_config_item` i
            LEFT JOIN `' . _DB_PREFIX_ . 'pt_config_tab` t ON (i.`id_tab` = t.`id_tab`)
            LEFT JOIN `' . _DB_PREFIX_ . 'pt_theme_editor` e ON (t.`id_editor` = e.`id_editor`)
            LEFT JOIN `' . _DB_PREFIX_ . 'pt_cms` c ON (e.`id_cms` = c.`id_cms`)
            WHERE i.`id_item` = '. (int)$id_item;
        if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql)) {
            // Check cases where there the item have a parent
            if (empty($row['id_tab'])) {
                $id_tab = (int)$row['id_tab'];
                $id_parent = (int)$row['id_parent'];
                $id_cms = (int)$row['id_cms'];
                
                // Take parent label until rich parent tab
                while (!$id_tab && $id_parent) {
                    $parent = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
                        SELECT i.`id_item`, i.`id_tab`, i.`id_parent`, c.`id_cms`
                        FROM `' . _DB_PREFIX_ . 'pt_config_item` i
                        LEFT JOIN `' . _DB_PREFIX_ . 'pt_config_tab` t ON (i.`id_tab` = t.`id_tab`)
                        LEFT JOIN `' . _DB_PREFIX_ . 'pt_theme_editor` e ON (t.`id_editor` = e.`id_editor`)
                        LEFT JOIN `' . _DB_PREFIX_ . 'pt_cms` c ON (e.`id_cms` = c.`id_cms`)
                        WHERE i.`id_item` = '. $id_parent);
                    $id_tab = (int)$parent['id_tab'];
                    $id_parent = (int)$parent['id_parent'];
                    $id_cms = (int)$parent['id_cms'];
                }
            } else {
                $id_cms = (int)$row['id_cms'];
            }
        }
        return $id_cms;
    }
    
    public function add($autodate = true, $null_values = false) {
        return parent::add($autodate, true);
    }
    
    public function update($null_values = false) {
        return parent::update(true);
    }
}